package com.goldman.aplt.base.core;

import com.goldman.aplt.base.constant.ApConstants;
import com.goldman.aplt.base.exception.BankBusiException;
import com.goldman.aplt.base.type.BaseEnumType;
import com.goldman.aplt.base.type.CmErrorCode;
import com.goldman.aplt.util.CommUtil;
import com.goldman.aplt.util.JsonNodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description：
 * @author： chenxiangxiang
 * @create： 2024/5/8 13:40
 */
@Component
@Slf4j
public class CoreVirAcctService {

    @Autowired
    private CoreUtil coreUtil;

    /**
     * 开通虚拟子账户
     */
    public String openVirAcct(String acctName, String clientNo, String name, String idcard, String phone, String currDate) {
        Map<String, Object> req = new HashMap<>();
        req.put("acctClass", "1");
        req.put("acctName", acctName);
        req.put("acctNature", "11004");
        req.put("acctOpenDate", currDate);
        req.put("actualRate", 0);

        req.put("allDepInd", "N");
        req.put("allDraInd", "N");
        req.put("altAcctName", "");
        req.put("baseAcctNo", "");
        req.put("ccy", ApConstants.BASE_CCY);

        req.put("clientNo", clientNo);
        req.put("effectDate", currDate);
        req.put("intClass", "INT");
        req.put("intType", "HQI");
        req.put("ownershipType", "SG");

        req.put("prefix", "");
        req.put("prodType", BaseEnumType.E_COREPRODTYPE.CVIRACCT.getValue());
        req.put("realRate", 0);
        req.put("reasonCode", "5");
        req.put("specAcctFlag", "N");

        req.put("acctRiskLevel", "1");
        req.put("checkCertificateAmt", "");

        List<Map<String, Object>> contactArray = new ArrayList<>();
        Map<String, Object> contactArrayMap = new HashMap<>();
        contactArrayMap.put("linkmanType", "11");
        contactArrayMap.put("linkmanName", name);
        contactArrayMap.put("documentType", "Y");
        //需要加密
        contactArrayMap.put("documentId", idcard);
        contactArrayMap.put("phoneNo1", phone);
        contactArray.add(contactArrayMap);

        List<Map<String, Object>> withdrawArray = new ArrayList<>();
        Map<String, Object> withdrawArrayMap = new HashMap<>();
        withdrawArrayMap.put("channelMuster", "MT");
        withdrawArrayMap.put("withdrawalType", "W");
        withdrawArray.add(withdrawArrayMap);

        req.put("contactArray", contactArray);
        req.put("withdrawArray", withdrawArray);

        CoreRespone resp = coreUtil.post("/rb/nfin/current/open", req);

        if (CommUtil.isNull(resp.getBody()) || CommUtil.isNull(resp.getBody().get("baseAcctNo"))) {
            throw new BankBusiException(CmErrorCode.CORE_SYS_ERROR);
        }

        return JsonNodeUtil.toString(resp.getBody().get("baseAcctNo"));
    }

    /**
     * 账户组建立
     */
    public String acctGroupCreate(String aioAcctNo, String groupName) {
        Map<String, Object> req = new HashMap<>();
        req.put("baseAcctNo", aioAcctNo);
        req.put("acctSeqNo", "1");
        req.put("prodType", BaseEnumType.E_COREPRODTYPE.CCURRENT.getValue());
        req.put("acctCcy", ApConstants.BASE_CCY);
        req.put("createGroupOperateType", "01");

        req.put("bothPartNature", "B");
        req.put("groupCcy", ApConstants.BASE_CCY);
        req.put("groupId", "");
        req.put("groupName", groupName);
        CoreRespone resp = coreUtil.post("/rb/nfin/pcp/group/create", req);

        if (CommUtil.isNull(resp.getBody()) || CommUtil.isNull(resp.getBody().get("pcpGroupId"))) {
            throw new BankBusiException(CmErrorCode.CORE_SYS_ERROR);
        }

        return JsonNodeUtil.toString(resp.getBody().get("pcpGroupId"));
    }

    /**
     * 账户组添加虚拟子账户
     */
    public CoreRespone acctGroupAddVirAcct(String groupId, String groupName, String subAcct, String currDate) {
        Map<String, Object> req = new HashMap<>();
        req.put("groupCcy", ApConstants.BASE_CCY);
        req.put("pcpGroupId", groupId);
        req.put("groupName", groupName);
        //01进行增加操作，03删除操作
        req.put("agreementOperateType", "01");

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("acctSeqNo", "1");
        map.put("baseAcctNo", subAcct);
        map.put("acctCcy", ApConstants.BASE_CCY);
        map.put("effectDate", currDate);
        map.put("inOutFlag", "");
        map.put("prodType", BaseEnumType.E_COREPRODTYPE.CVIRACCT.getValue());
        map.put("incExpInd", "B");
        map.put("priority", "01");
        map.put("subSeqNo", "1");
        list.add(map);

        req.put("subInfoArray", list);

        return coreUtil.post("/rb/nfin/pcp/group/maint", req);
    }

    /**
     * 资金池首次签约
     */
    public String capitalPoolFirstSign(String aioAcctNo, String groupId, String groupName) {
        String currDate = CommUtil.getComputerDate();
        Map<String, Object> req = new HashMap<>();
        req.put("agreementOperateType", "01");

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("acctSeqNo", "1");
        map.put("baseAcctNo", aioAcctNo);
        map.put("ccy", ApConstants.BASE_CCY);
        map.put("createDate", currDate);
        map.put("pcpGroupId", groupId);
        map.put("groupName", groupName);
        map.put("prodType", BaseEnumType.E_COREPRODTYPE.CCURRENT.getValue());
        list.add(map);
        req.put("acctGroupArray", list);

        req.put("cashPoolAttr", "1");
        req.put("crRate", 0);
        req.put("cycCtrl", "Y");
        req.put("downMethod", "RA");
        req.put("downPlan", "L01");

        req.put("drRate", 0);
        req.put("paymentFlag", "D");
        req.put("paymentMethod", "RQ");
        req.put("paymentPlan", "Q01");
        req.put("effectDate", currDate);

        req.put("innerPrice", "N");
        req.put("innerPriceMode", "S");
        req.put("innerPriceWay", "N");
        req.put("limitMode", "01");
        req.put("openCtrl", "Y");

        req.put("openLimit", "N");
        req.put("option", "01");
        req.put("pcpProdType", "02020104");
        req.put("tranDate", currDate);
        req.put("upMethod", "CC");

        //凌晨一点归集
        req.put("upTime", ApConstants.UP_TIME);
        req.put("upPlan", "D02");
        req.put("upRemainAmt", "0");
        CoreRespone resp = coreUtil.post("/rb/nfin/pcp/sign", req);

        if (CommUtil.isNull(resp.getBody()) || CommUtil.isNull(resp.getBody().get("agreementId"))) {
            throw new BankBusiException(CmErrorCode.CORE_SYS_ERROR);
        }

        return JsonNodeUtil.toString(resp.getBody().get("agreementId"));
    }

    /**
     * 资金池维护签约
     */
    public CoreRespone capitalPoolUpdateSign(String aioAcctNo, String groupId, String agreementId, String virSubAcctNo, String currDate) {
        Map<String, Object> req = new HashMap<>();
        req.put("agreementOperateType", "01");
        req.put("baseAcctNo", aioAcctNo);
        req.put("ccy", ApConstants.BASE_CCY);
        req.put("prodType", BaseEnumType.E_COREPRODTYPE.CCURRENT.getValue());
        req.put("acctSeqNo", "0");

        req.put("pcpGroupId", groupId);
        req.put("effectDate", currDate);
        req.put("agreementId", agreementId);

        req.put("paymentFlag", "D");
        req.put("paymentMethod", "RQ");
        req.put("paymentPlan", "Q01");

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("pcpGroupId", groupId);
        map.put("agreementId", agreementId);
        map.put("acctSeqNo", "1");
        map.put("baseAcctNo", virSubAcctNo);
        map.put("ccy", ApConstants.BASE_CCY);

        map.put("crRate", "");
        map.put("cycCtrl", "Y");
        map.put("downFreq", "");
        map.put("downDay", "");
        map.put("downMethod", "RA");

        map.put("downPlan", "L01");
        map.put("drRate", "");
        map.put("effectDate", currDate);
        map.put("innerPrice", "N");
        map.put("openCtrl", "Y");

        map.put("overLimitAmt", "0.0");
        map.put("ownLimit", "0.0");
        map.put("prodType", BaseEnumType.E_COREPRODTYPE.CVIRACCT.getValue());
        map.put("seqNo", "");
        map.put("upFreq", "D");

        map.put("upTime", ApConstants.UP_TIME);
        map.put("upMethod", "CC");
        map.put("upPlan", "D02");
        map.put("upRemainAmt", "0");

        map.put("paymentFlag", "D");
        map.put("paymentMethod", "RQ");
        map.put("paymentPlan", "Q01");
        list.add(map);

        req.put("tranArray", list);
        return coreUtil.post("/rb/nfin/pcp/acct/maint", req);
    }
}
